package com.andy.dogcoin;

import android.Manifest;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.provider.Settings;
import android.view.View;

import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;

import com.andy.utils.UIUtil;
import com.tbruyelle.rxpermissions3.RxPermissions;

import io.reactivex.rxjava3.functions.Consumer;


/**
 * Created by Andy on  2024/4/24 1:09
 * Email: 963069079@qq.com
 */
public class WelComeActivity extends AppCompatActivity
{

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_wel_come);
        requestPermission();
    }
    public void enter(View v){
        Intent i = new Intent(this,MainActivity.class);
        startActivity(i);
        this.finish();
    }

    private void requestPermission() {



        RxPermissions rxPermissions = new RxPermissions(this);
        rxPermissions.request(android.Manifest.permission.WRITE_EXTERNAL_STORAGE
                , Manifest.permission.READ_EXTERNAL_STORAGE
//                , Manifest.permission.MANAGE_EXTERNAL_STORAGE

        ).subscribe(new Consumer<Boolean>() {
            @RequiresApi(api = Build.VERSION_CODES.R)
            @Override
            public void accept(Boolean aBoolean) throws Exception {
                if (aBoolean) {
                    UIUtil.showToast(WelComeActivity.this,"请求权限成功");
//                    FilesUtil.getAppDbPath();
//                    DBManager.getInstance(WelComeActivity.this);
                    setting(WelComeActivity.this);
                } else {
                    UIUtil.showToast(WelComeActivity.this,"请求权限失败");
                }
            }
        });
    }


    // 跳转至设置页面，让用户手动开启
    @RequiresApi(api = Build.VERSION_CODES.R)
    public void setting(Activity activity) {

            if (!Environment.isExternalStorageManager()) {
                Intent intent = new Intent(
                        Settings.ACTION_MANAGE_APP_ALL_FILES_ACCESS_PERMISSION);
                intent.setData(Uri.parse("package:" + activity.getPackageName()));
                activity.startActivity(intent);
            }

    }
}

